//
//  HHYZiXuanVC.m
//  大股票
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 apple. All rights reserved.
//

#import "HHYZiXuanVC.h"

//V
#import "HHYTopTitleView.h"
//VC
#import "HHYStockListVC.h"

@interface HHYZiXuanVC ()<UIScrollViewDelegate>
{
    NSInteger _selectedTopTitleViewType;//显示模式
}

@property (nonatomic, strong) HHYTopTitleView *topTitleView;

@property (nonatomic, strong) UIButton *statusBtn;
//股票列表数组
@property (nonatomic, strong) HHYStockListVC *listVC;

@end

@implementation HHYZiXuanVC

- (instancetype)init {
    self = [super init];
    if (self) {
        _selectedTopTitleViewType = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"自选";
    //子view
    [self setupSubViews];
    [self updateUI];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    [self updateSubViewFrame];
}

- (void)setupSubViews {
    [self.view addSubview:self.topTitleView];
    [self addChildVC];
    [self.listVC.view addSubview:self.statusBtn];
}

- (void)updateUI {
    [self changeTopViewTitleArrWithIndex:0];
}

- (void)updateSubViewFrame {
    //总高度 （含 导航栏及tabbar）
    CGFloat totalHeight = hhy_kScreenHeight - hhy_kStatusBarAndNavigationBarHeight - hhy_kTabbarHeight;
    
    self.topTitleView.xl_y = 0;
    
    
    CGFloat sideslipViewOriY = CGRectGetMaxY(self.topTitleView.frame);
    self.listVC.view.frame = CGRectMake(0, sideslipViewOriY, hhy_kScreenWidth, totalHeight - sideslipViewOriY);
    
    self.statusBtn.frame = CGRectMake(self.listVC.view.xl_width - 56 - 15, self.listVC.view.xl_height - 56 - 15, 56, 56);
}

#pragma mark - 加入子vc
- (void)addChildVC {
    
    [self.view addSubview:self.listVC.view];
    
    [self addChildViewController:self.listVC];
    
}

#pragma mark - btnActions
- (void)btnActions:(UIButton *)btn {
    if (@available(iOS 11.0, *)) {
        //防止多次触发事件
        __block UIButton *blockBtn = btn;
        blockBtn.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            blockBtn.userInteractionEnabled = YES;
        });
    }
    
    if (btn == self.statusBtn) {
        NSInteger index = _selectedTopTitleViewType==0?1:0;
        [self changeTopViewTitleArrWithIndex:index];
        return;
    }
    
}

#pragma mark - UIScrollViewDelegate

#pragma mark - 切换toptitleView 标题
- (void)changeTopViewTitleArrWithIndex:(NSInteger)index {
    _selectedTopTitleViewType = index;
    switch (index) {
        case 0:
        {
            self.topTitleView.titleArr = @[@"交易产品", @"卖出价", @"买入价"];
            self.listVC.displayType = HHYStockListTCellDisplayType1;
        }
            break;
        case 1:
        {
            self.topTitleView.titleArr = @[@"交易产品", @"最新价", @"涨跌幅(%)"];
            self.listVC.displayType = HHYStockListTCellDisplayType2;
        }
            break;
        default:
            break;
    }

    
    
}

#pragma mark - getter
- (HHYStockListVC *)listVC {
    if (!_listVC) {
        _listVC = [[HHYStockListVC alloc] initWithType:HHYStockListVCTypeMarket displayType:HHYStockListTCellDisplayType1 shouldShowPlaceHodlerView:YES];
        _listVC.view.frame = self.view.bounds;
        __weak typeof(self)wSelf = self;
        _listVC.marketHandler = ^{
            __strong typeof(self)sSelf = wSelf;
            //跳转至行情
            [sSelf.tabBarController setSelectedIndex:1];
        };
    }
    return _listVC;
}

- (HHYTopTitleView *)topTitleView {
    if (!_topTitleView) {
        _topTitleView = [[HHYTopTitleView alloc] initWithFrame:CGRectMake(0, 0, hhy_kScreenWidth, 40)];
    }
    return _topTitleView;
}

- (UIButton *)statusBtn {
    if (!_statusBtn) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _statusBtn.frame = CGRectMake(8, 23, 56, 56);
        //        _statusBtn.tintColor = WHITE_COLOR;
        [_statusBtn setBackgroundImage:[[UIImage imageNamed:@"切换查看方式"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [_statusBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _statusBtn;
}

@end
